package org.sgx.gwteditors.client.impl1.simple;

import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.EditorFramework;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Widget;

public class BooleanEditor extends CheckBox implements Editor<Boolean>, 
ValueChangeHandler<Boolean> {

	@Override
	public boolean isSimple() {
		return true;
	}
	
	public BooleanEditor() {
		super();
		addValueChangeHandler(this);
	}

	@Override
	public Boolean flush() {
		return getValue();//no validation / errors
	}

	@Override
	public void load(Boolean t) {
//		this.model = t;
		setValue(t);
	}
	@Override
	public Widget getWidget() {
		return this;
	}
	
	@Override
	public void addValueChangeListener(ValueChangeListener<Boolean> l) {
		EditorEventManager.<Boolean>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<Boolean> l) {
		EditorEventManager.<Boolean>getInstance().removeValueChangeListener(this, l);
	}

	@Override
	public void onValueChange(ValueChangeEvent<Boolean> event) {
		EditorEventManager.<Boolean>getInstance().fireValueChangeGwtEvent(this, event);	
	}

	@Override
	public List<EditorError<Boolean>> getErrors() {
		return EditorErrorManager.<Boolean>getInstance().getErrors(this);
	}

	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[]{EditorFramework1.TYPE_BOOLEAN}; 
	}
}
